package com.taorenqi.business.fragment.login;


import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.taorenqi.business.R;
import com.taorenqi.business.event.StartFragmentEvent;
import com.taorenqi.business.fragment.base.BaseBackFragment;
import com.taorenqi.business.fragment.main.MainFragment;
import com.taorenqi.business.fragment.mine.RechargeFragment;
import com.taorenqi.business.utils.ToastUtil;
import com.taorenqi.business.widget.statusview.StatusView;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 注册成功
 * A simple {@link Fragment} subclass.
 */
public class RegisterSuccessfullyFragment extends BaseBackFragment {


    @Bind(R.id.status_view)
    StatusView statusView;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.tv_register_success_todo_buy)
    TextView tvRegisterSuccessTodoBuy;
    @Bind(R.id.tv_register_success_todo_publish)
    TextView tvRegisterSuccessTodoPublish;
    @Bind(R.id.tv_register_success_todo_qq)
    TextView tvRegisterSuccessTodoQq;
    @Bind(R.id.activity_register_success_todo)
    LinearLayout activityRegisterSuccessTodo;

    public static RegisterSuccessfullyFragment newInstance() {
        // Required empty public constructor
        RegisterSuccessfullyFragment fragment = new RegisterSuccessfullyFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_register_successfully, container, false);
        ButterKnife.bind(this, view);
        toolbar.setTitle("注册成功");
        initToolbarNav(toolbar);
        return attachToSwipeBack(view);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.tv_register_success_todo_buy, R.id.tv_register_success_todo_publish, R.id.tv_register_success_todo_qq})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_register_success_todo_buy://购买G币
                start(RechargeFragment.newInstance());
                break;
            case R.id.tv_register_success_todo_publish://发布任务
                start(MainFragment.newInstance());
                break;
            case R.id.tv_register_success_todo_qq://联系客服
                if(isQQClientAvailable(_mActivity)){
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://wpa.b.qq.com/cgi/wpa.php?ln=2&uin=4006331298")));
                }else{
                    ToastUtil.showToast(_mActivity,"请安装QQ");
                }
                break;
        }
    }


    public static boolean isQQClientAvailable(Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mobileqq")) {
                    return true;
                }
            }
        }
        return false;
    }
}
